<template>
  <button @click="open()">打开弹窗</button>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="昵称"></el-table-column>
    <el-table-column prop="permissionName" label="菜单权限" width="500px"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogFormVisible" :before-close="beforClose" title="添加权限" width="500">
    <el-form ref="formRef" label-width="100px" label-position="left " :model="form" :rules="rules">
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="填写权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permission">
        <el-tree
          style="max-width: 600px"
          :data="permissionData"
          node-key="id"
          show-checkbox
          :default-checked-keys="defaultKeys"
          :default-expanded-keys="[2]"
          ref="treeRef"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, onMounted, nextTick } from 'vue'
  import { userGetMenu, userSetMenu, menuList } from '../../../api'

  onMounted(() => {
    // 菜单数据
    userGetMenu().then(({ data }) => {
      console.log(data)
      permissionData.value = data.data
      getListData()
    })
  })

  const paginationData = reactive({
    pageNum: 1,
    pageSize: 10,
  })

  // 列表数据
  const tableData = reactive({
    list: [],
    total: 0,
  })

  // 打开弹窗
  const open = (rowData = {}) => {
    dialogFormVisible.value = true
    // 弹窗打开form生成是异步的
    nextTick(() => {
      Object.assign(form, { id: rowData.id, name: rowData.name })
      treeRef.value.setCheckedKeys(rowData.permission)
    })
  }
  // 请求列表数据
  const getListData = () => {
    menuList(paginationData).then(({ data }) => {
      const { list, total } = data.data
      tableData.list = list
      tableData.total = total
    })
  }

  const formRef = ref()

  // 表单数据
  const form = reactive({
    id: '',
    name: '',
    permission: '',
  })

  // 树形菜单权限数据
  const permissionData = ref([])

  // 弹窗的显示
  const dialogFormVisible = ref(false)

  // 关闭弹窗的回调
  const beforClose = () => {
    dialogFormVisible.value = false
    // 重置表单
    formRef.value.resetFields()
    // 重置选择
    treeRef.value.setCheckedKeys(defaultKeys)
  }

  // 选中权限
  const defaultKeys = [4, 5]

  const treeRef = ref()

  const rules = reactive({
    name: [{ required: true, trigger: 'blur', message: '请输入权限名称' }],
  })

  //表单提交
  const confirm = async formEl => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
      if (valid) {
        // 获取选择的checkbox的数据
        const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
        userSetMenu({ name: form.name, permissions, id: form.id }).then(({ data }) => {
          console.log(data)
        })
      } else {
        console.log('error submit!', fields)
      }
    })
  }
</script>

<style lang="scss" scoped></style>
